﻿using Common;
using MuseLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MuseUI
{
    [DataContract]
    public class UserContext
    {
        public void Save()
        {
            using (var file = new StreamWriter("usercontext.xml"))
            {
                var serialisedUserContext = GenericSerialiser.Serialize(this);
                file.Write(serialisedUserContext);
            }
        }

        private void ThreadedSave()
        {
            var thread = OneLineThread.Start(() =>
                {
                    lock (LockSave)
                    {
                        Save();
                    }
                });
        }

        public static UserContext Load()
        {
            UserContext userContext;
            try
            {
                using (var file = new StreamReader("usercontext.xml"))
                {
                    var xmlString = file.ReadToEnd();
                    userContext = (UserContext)GenericSerialiser.Deserialize(xmlString, typeof(UserContext));
                }
            }
            catch
            {
                File.Delete("usercontext.xml");
                userContext = new UserContext();
            }

            return userContext;
        }

        [DataMember(Name = "MuseAddress")]
        private String _museAddress = "";
        [DataMember(Name = "AutoConnect")]
        private bool _autoConnect = false;
        [DataMember(Name = "ChartUpdateFrequency")]
        private double _chartUpdateFrequency = 20;
        [DataMember(Name = "SelectedMuseChartType")]
        private MuseChartType _museChartType = MuseChartType.Eeg;
        [DataMember(Name = "NotchFrequency")]
        private int _notchFrequency = 60;

        private object _lockSave = new object();
        public object LockSave
        {
            get
            {
                if (_lockSave == null)
                    _lockSave = new object();
                return _lockSave;
            }
        }


        public double ChartUpdateFrequency
        {
            get { return _chartUpdateFrequency; }
            set
            {
                _chartUpdateFrequency = value;
                ThreadedSave();
            }
        }

        public bool AutoConnect
        {
            get { return _autoConnect; }
            set
            {
                _autoConnect = value;
                ThreadedSave();
            }
        }

        public String MuseAddress
        {
            get { return _museAddress; }
            set
            {
                _museAddress = value;
                ThreadedSave();
            }
        }

        public MuseChartType MuseChartType
        {
            get { return _museChartType; }
            set
            {
                _museChartType = value;
                ThreadedSave();
            }
        }

        public int NotchFrequency
        {
            get { return _notchFrequency; }
            set
            {
                _notchFrequency = value;
                ThreadedSave();
            }
        }

    }
}
